package com.zwz.crawler.mangeInfo.invertPage;

import com.zwz.constant.Constant;
import com.zwz.parse.JsonParserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.bcel.Const;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import static com.zwz.constant.Constant.ENTERPRISE_ID;

@Slf4j
public class TungeeInvestmentQuery {
//    private static final String ENTERPRISE_ID = "577de19eadf21dc4";
    private static final int START = 0;
    private static final int END = 10;

    public static int StartTungeeInvestmentQuery(String ENTERPRISE_ID) {
        try {
            // 构建严格编码的URL
            String encodedId = URLEncoder.encode(ENTERPRISE_ID, String.valueOf(StandardCharsets.UTF_8));
            String url = String.format(
                    "https://sales.tungee.com/api/enterprise/detail/invest-page?enterprise_id=%s&start=%d&end=%d",
                    encodedId, START, END
            );

            // 发送严格配置的请求
            Connection.Response response = Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    .header("authority", "sales.tungee.com")
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br, zstd")
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("cookie", Constant.cookies)
                    .header("pragma", "no-cache")
                    .header("priority", "u=1, i")
                    .header("referer", "https://sales.tungee.com/enterprise-details/" + encodedId + "/enterprise-information/basic-information")
                    .header("sec-ch-ua", "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36")
                    .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                    .ignoreContentType(true)
                    .ignoreHttpErrors(true)
                    .timeout(30000)
                    .execute();

            // 输出原始响应
           log.info("HTTP状态码: " + response.statusCode());
           log.info("响应内容:\n" + response.body());


            //为了统一这里错了基本判断todo 待以后优化
//            if (!response.body().equals("")) {
//                return 3;
//            }

            //解析
            int intres = 0;
            intres = JsonParserUtil.parseInsevet(response.body());
            return intres;

        } catch (IOException e) {
            System.err.println("请求失败: " + e.getMessage());
        }
        return 0;
    }

}




